import {Component, Input, OnInit} from '@angular/core';
import {SFSchema} from "@delon/form";
import {map} from "rxjs/operators";
import {isTemplateRef, NzMessageService, NzModalRef, UploadFile} from "ng-zorro-antd";
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {_HttpClient} from "@delon/theme";
import {ApiService} from "@core/services/api";
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-room-edit',
  templateUrl: './room-edit.component.html',
  styles: []
})
export class RoomEditComponent implements OnInit {
  @Input()type: number; // 0 新增 1 修改
  @Input()set _param(value) {
    this.param = value;
  }; // 参数
  param: any
  showForm = false;
  validateForm!: FormGroup;
  schema: SFSchema = {
    properties: {
      roomName: {
        type: 'string',
        title: '房间名',
        ui: {
          grid: {span: 8}
        }

      },
      roomNumber: {
        type: 'string',
        title: '门牌号',
        ui: {
          grid: {span: 8}
        }
      },
      roomFloor: {
        type: 'string',
        title: '所在楼层',
        ui: {
          grid: {span: 8}
        }
      },
      roomAcreage: {
        type: 'string',
        title: '房间面积',
        ui: {
          grid: {span: 8}
        }
      },
      roomDecoration: {
        type: 'string',
        title: '装修描述',
        ui: {
          grid: {span: 8}
        }
      },
      roomOrientation: {
        type: 'string',
        title: '朝向描述',
        ui: {
          grid: {span: 8}
        }
      },
      roomCode:{
        type:'string',
        title:'房间编码',
        ui: {
          grid: {span: 8}
        }
      },
      houseType:{
        type:'string',
        title:"房屋类别",
        enum:[
          {label:'单元楼、公寓楼',value:'10'},
          {label:'筒子楼',value:'20'},
          {label:'别墅',value:'30'},
          {label:'自建楼',value:'40'},
          {label:'平房',value:'50'},
          {label:'四合院',value:'60'},
          {label:'其它',value:'90'},
        ],
        ui:{
          widget:'select',
          allowClear:true,
          grid:{span:8}
        }
      },
      houseYtdm:{
        type:'string',
        title:'房屋用途',
        enum:[],
        ui:{
          widget:'select',
          allowClear:true,
          grid:{span:8}
        }
      },
      houseBdcqzs:{
        type:'string',
        title:'不动产权证书',
        ui: {
          grid: {span: 8}
        }
      },
      houseFwcqxz:{
        type:'string',
        title:'房屋产权性质',
        enum:[
          {label:'国有房产',value:'10'},
          {label:'集体所有房产',value:'20'},
          {label:'私有房产',value:'30'},
          {label:'联营企业房产',value:'40'},
          {label:'股份制企业房产',value:'50'},
          {label:'港、澳、台胞房产',value:'60'},
          {label:'涉外房产',value:'70'},
          {label:'其它',value:'90'},
        ],
        ui:{
          widget:'select',
          allowClear:true,
          grid:{span:8}
        }
      },
      houseCqrXm:{
        type:'string',
        title:'产权人(单位)_姓名',
        ui: {
          grid: {span: 8}
        }
      },
      houseCqrGjdq:{
        type:'string',
        title:'产权人(单位)_国籍(地区)',
        enum:[],
        ui: {
          widget:'select',
          allowClear:true,
          grid: {span: 8}
        }
      },
      houseCqrCyzj:{
        type:'string',
        title:'产权人(单位)_证件种类',
        enum:[],
        ui: {
          widget:'select',
          allowClear:true,
          grid: {span: 8}
        }
      },
      houseCqrZjhm:{
        type:'string',
        title:'产权人(单位)_证件号码',
        ui: {
          grid: {span: 8}
        }
      },
      houseCqrWwx:{
        type:'string',
        title:'产权人(单位)_外文姓',
        ui: {
          grid: {span: 8}
        }
      },
      houseCqrWwm:{
        type:'string',
        title:'产权人(单位)_外文名',
        ui: {
          grid: {span: 8}
        }
      },
      houseCqrLxdh:{
        type:'string',
        title:'产权人(单位)_联系电话',
        ui: {
          grid: {span: 8}
        }
      },
      roomRemark: {
        type: 'string',
        title: '备注',
        ui: {
          widget: 'textarea',
          grid: {span: 8}
        }
      }

    },
    "ui": {
      spanLabel:10,
      spanControl:14,
      grid: {
        "span": 8
      }
    },
    required: ['roomName' , 'roomFloors', 'roomNumber']
  }
  batchAdd = {
    startFloor: null,
    endFloor: null,
    startRoom: null,
    endRoom: null
  }
  //单个
  submit(data: object): void {
    const url = this.type === 0 ? this.api.room.add.url : this.api.room.update.url+ `/${data['id']}`;
    const method = this.type === 0 ? this.api.room.add.method : this.api.room.update.method;
    data['areaId'] = this.param.areaId;
    data['areaParentAllId'] = this.param.areaParentAllId
    this.http.request<any>(method, url, {body:data}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success(res.message);
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  //批量
  submitForm(data): void {
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
      if(this.validateForm.controls[key].invalid){
        return;
      }
    }
    const url = this.api.room.add.url + '/addRoomList';
    const body = {
      startFloor: data.startFloor,
      terminationFloor: data.endFloor,
      startNumber: data.startRoom,
      terminationNumber: data.endRoom,
      areaId: this.param.areaId
    }
    this.http.post(url, body).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success(res.message);
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }

  constructor(
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private area: AreaService,
    private http: _HttpClient,
    private api: ApiService,
    private fb: FormBuilder
  ) { }

  ngOnInit() {
    this.getDictionary().then(res=>{
      this.http.post(this.api.dictionary.page.url,{dictionaryType:'GJHDQ'}).subscribe(res=>{
        if(res.status == 200){
          this.schema.properties.houseCqrGjdq.enum=res.data.list.map(item =>{
            return {value:item.dictionaryCode,label:item.dictionaryValue}
          })
        }
        this.showForm=true;
      })
    })

    if(this.param.addType == 1){
      this.validateForm = this.fb.group({
        startFloor:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
        endFloor:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
        startRoom:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
        endRoom:["",[Validators.required,Validators.pattern(/^[a-zA-Z]+$|^[0-9]+$/)]],
      });
    }
  }

  getDictionary(){
    return new Promise(resolve => {
      this.http.post(this.api.dictionary.page.url,{dictionaryType:'CYZJ'}).subscribe(res => {
        if(res.status == 200){
          this.schema.properties.houseCqrCyzj.enum=res.data.list.map((item) =>{
            return {value:item.dictionaryCode,label:item.dictionaryValue};
          })
        }
        this.http.post(this.api.dictionary.page.url,{dictionaryType:'FWYTDM'}).subscribe(res=>{
          if(res.status == 200){
            this.schema.properties.houseYtdm.enum=res.data.list.map(item =>{
              return {value:item.dictionaryCode,label:item.dictionaryValue}
            })
          }
          resolve(res);
        })
       
      })
    })
    
  }

}
